<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table class="treeTable" :data="catelist" :columns="columns" :selection-type="false" :expand-type="false" show-index border>
        <!-- 是否有效 -->
        <template v-slot:isok="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color:lightgreen;"></i>
          <i class="el-icon-error" v-else style="color:red;"></i>
        </template>
        <!-- 排序 -->
        <template v-slot:order="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template v-slot:opt="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGood(scope.row.cat_id)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="open(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <!-- <add-cate @changeTitle="changeTitle($event)"></add-cate> -->
      <router-view @changeTitle="changeTitle($event)" @refs="setRefs($event)" @close="closeDialog($event)" :id="goodsid" :ref="refs"></router-view>
      <span slot="footer" class="dialog-footer"></span>
    </el-dialog>
  </div>
</template>

<script>
// import Addcate from '../goods/Addcate.vue'
export default {
  components: {
    // 'add-cate': Addcate
  },
  data() {
    return {
      // 商品分类的数据列表
      catelist: [],
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 总数据条数
      total: 0,
      // 为table指定列的定义
      columns: [{
        label: '分类名称',
        prop: 'cat_name'

      }, {
        label: '是否有效',
        // 表示将当前列定义为模板列
        type: 'template',
        // 表示当前这一列使用的模板名称
        template: 'isok'
      }, {
        label: '排序',
        // 表示将当前列定义为模板列
        type: 'template',
        // 表示当前这一列使用的模板名称
        template: 'order'
      }, {
        label: '操作',
        // 表示将当前列定义为模板列
        type: 'template',
        // 表示当前这一列使用的模板名称
        template: 'opt'
      }],
      dialogVisible: false,
      // 对话框标题
      title: '添加',
      // 传给子组件的id
      goodsid: 0,
      // 子组件传递的refs
      refs: ''
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      console.log(res.data)
      this.catelist = res.data.result
      this.total = res.data.total
    },
    // 监听pagesize改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听pagenum的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    // 按下添加按钮之后的事件
    addCate() {
      const that = this
      this.$router.push({ path: '/addcate' })
      that.dialogVisible = true
    },
    changeTitle(e) {
      const that = this
      that.title = e
    },
    // 点击编辑按钮时传入id
    editGood(id) {
      const that = this
      console.log(id)
      this.goodsid = id
      // 打开对话框
      this.$router.push({ path: '/editcate' })
      that.dialogVisible = true
      if (this.refs === 'editCateRef') {
        this.$refs.editCateRef.getCategories(id)
      }
    },
    // 子组件传递的关闭对话框事件
    closeDialog(e) {
      this.dialogVisible = e
      this.getCateList()
    },
    // 关闭对话框时的事件
    handleClose() {
      if (this.refs === 'addCateRef') {
        this.$refs.addCateRef.closeDialog()
      } else if (this.refs === 'editCateRef') {
         this.$refs.editCateRef.closeDialog()
      }
    },
    // 获取子组件传递的refs
    setRefs(e) {
      this.refs = e
    },
    // 点击删除按钮触发的事件
    deleteGood(id) {
      const that = this
      console.log(id)
      this.goodsid = id
      this.$router.push({ path: '/deletecate' })
      that.dialogVisible = true
    },
    open(id) {
        const that = this
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const { data: res } = await this.$http.delete('categories/' + id)
          console.log(res)
          if (res.meta.status !== 200) {
            return this.$message({
            type: 'fail',
            message: res.meta.msg + '!'
          })
          }
          this.$message({
            type: 'success',
            message: res.meta.msg + '!'
          })
          that.getCateList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
  }

}
</script>

<style>
.treeTable{
  margin-top: 15px;
}
</style>
